<template>
  <Teleport to="body">
    <div v-if="visible" class="message-container">
      <div :class="['message', `message-${type}`]">
        <div class="message-icon" v-if="type">
          <svg v-if="type === 'success'" class="h-5 w-5" viewBox="0 0 20 20" fill="currentColor">
            <path fill-rule="evenodd" d="M10 18a8 8 0 100-16 8 8 0 000 16zm3.707-9.293a1 1 0 00-1.414-1.414L9 10.586 7.707 9.293a1 1 0 00-1.414 1.414l2 2a1 1 0 001.414 0l4-4z" clip-rule="evenodd" />
          </svg>
          <svg v-if="type === 'error'" class="h-5 w-5" viewBox="0 0 20 20" fill="currentColor">
            <path fill-rule="evenodd" d="M10 18a8 8 0 100-16 8 8 0 000 16zM8.707 7.293a1 1 0 00-1.414 1.414L8.586 10l-1.293 1.293a1 1 0 101.414 1.414L10 11.414l1.293 1.293a1 1 0 001.414-1.414L11.414 10l1.293-1.293a1 1 0 00-1.414-1.414L10 8.586 8.707 7.293z" clip-rule="evenodd" />
          </svg>
          <svg v-if="type === 'warning'" class="h-5 w-5" viewBox="0 0 20 20" fill="currentColor">
            <path fill-rule="evenodd" d="M8.257 3.099c.765-1.36 2.722-1.36 3.486 0l5.58 9.92c.75 1.334-.213 2.98-1.742 2.98H4.42c-1.53 0-2.493-1.646-1.743-2.98l5.58-9.92zM11 13a1 1 0 11-2 0 1 1 0 012 0zm-1-8a1 1 0 00-1 1v3a1 1 0 002 0V6a1 1 0 00-1-1z" clip-rule="evenodd" />
          </svg>
          <svg v-if="type === 'info'" class="h-5 w-5" viewBox="0 0 20 20" fill="currentColor">
            <path fill-rule="evenodd" d="M18 10a8 8 0 11-16 0 8 8 0 0116 0zm-7-4a1 1 0 11-2 0 1 1 0 012 0zM9 9a1 1 0 000 2v3a1 1 0 001 1h1a1 1 0 100-2v-3a1 1 0 00-1-1H9z" clip-rule="evenodd" />
          </svg>
        </div>
        <span class="message-content">{{ content }}</span>
      </div>
    </div>
  </Teleport>
</template>

<script setup lang="ts">
import { ref } from 'vue';

interface MessageOptions {
  content: string;
  type?: 'success' | 'error' | 'warning' | 'info';
  duration?: number;
}

const visible = ref(false);
const content = ref('');
const type = ref<'success' | 'error' | 'warning' | 'info'>('info');
let timer: NodeJS.Timeout | null = null;

const show = (options: MessageOptions) => {
  if (timer) {
    clearTimeout(timer);
    timer = null;
  }

  content.value = options.content;
  type.value = options.type || 'info';
  visible.value = true;

  const duration = options.duration ?? 3000;
  timer = setTimeout(() => {
    visible.value = false;
  }, duration);
};

defineExpose({
  show
});
</script>

<style scoped>
.message-container {
  position: fixed;
  top: 20px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 9999;
  pointer-events: none;
}

.message {
  display: flex;
  align-items: center;
  padding: 10px 16px;
  border-radius: 8px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
  background: white;
  margin-bottom: 8px;
  transition: all 0.3s ease;
  animation: messageSlideDown 0.3s ease;
}

/* 深色模式适配 */
:deep(.dark) .message {
  background: #1f2937;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.3);
}

:deep(.dark) .message-content {
  color: #e5e7eb;
}

:deep(.dark) .message-success {
  background: #064e3b;
  border: 1px solid #059669;
}

:deep(.dark) .message-error {
  background: #7f1d1d;
  border: 1px solid #dc2626;
}

:deep(.dark) .message-warning {
  background: #78350f;
  border: 1px solid #d97706;
}

:deep(.dark) .message-info {
  background: #1e3a8a;
  border: 1px solid #2563eb;
}

.message-icon {
  margin-right: 8px;
  display: flex;
  align-items: center;
}

.message-content {
  font-size: 14px;
  line-height: 1.5;
}

.message-success {
  background: #f0fdf4;
  border: 1px solid #86efac;
}

.message-success .message-icon {
  color: #22c55e;
}

.message-error {
  background: #fef2f2;
  border: 1px solid #fca5a5;
}

.message-error .message-icon {
  color: #ef4444;
}

.message-warning {
  background: #fffbeb;
  border: 1px solid #fcd34d;
}

.message-warning .message-icon {
  color: #f59e0b;
}

.message-info {
  background: #f0f9ff;
  border: 1px solid #7dd3fc;
}

.message-info .message-icon {
  color: #0ea5e9;
}

@keyframes messageSlideDown {
  from {
    opacity: 0;
    transform: translateY(-10px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

@media (max-width: 1024px) {
  .message {
    padding: 8px 12px !important;
    border-radius: 6px !important;
    margin-bottom: 6px !important;
  }

  .message-content {
    font-size: 12px !important;
  }

  .message-icon {
    margin-right: 6px !important;
  }

  .message-icon svg {
    width: 16px !important;
    height: 16px !important;
  }

  .message-container {
    top: 16px !important;
  }
}
</style>
